package cpm.kl.config;

import lombok.extern.slf4j.Slf4j;
import org.flywaydb.core.api.callback.BaseCallback;
import org.flywaydb.core.api.callback.Context;
import org.flywaydb.core.api.callback.Event;
import org.flywaydb.core.internal.jdbc.JdbcTemplate;
import org.springframework.context.annotation.Configuration;

import java.sql.SQLException;

//@Configuration
@Slf4j
public class ExampleFlywayCallback extends BaseCallback {
    @Override
    public void handle(Event event, Context context) {
        switch (event) {
            // 在每次成功迁移后触发。此事件将在与迁移相同的事务中触发
            case AFTER_EACH_MIGRATE:
                final JdbcTemplate jdbcTemplate = new JdbcTemplate(
                        context.getConnection());
                try {
                    jdbcTemplate.execute("insert into user (`name`, `age`) values (?,?)", "test", "20");
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
                log.info("触发迁移事件后执行");
                break;
            case BEFORE_UNDO:
                log.info("撤销操作之前触发");
        }
    }
}